﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace SPALM
{
    internal class UpgradeSolutionsOperation : DeploymentOperation
    {
        private string Solutions;

        private string[] solutionNames;
        private string[] solutionFileNames;

        public UpgradeSolutionsOperation(Deployment parentTask, string Solutions, int ParsedRetries, int ParsedWaitSecondsAfterFinish, bool ParsedSimulateDeployment)
        : base(parentTask, ParsedRetries, ParsedWaitSecondsAfterFinish, ParsedSimulateDeployment)
        {            
            // TODO: Complete member initialization
            this.Solutions = Solutions;
            
            //Parameter extraction
            solutionNames = GetSolutionNames(Solutions);
            solutionFileNames = GetArrayFromString(Solutions);
        }

        public override void CheckPreconditions()
        {
            LogMessage(FormatHeader("Check preconditions"), LogLevel.High);
            
            //1. Checks
            if(solutionFileNames.Length == 0)            
            {
                exceptions.Add("No solutions given in parameter 'Solutions'");
            }

            //a. Existieren alle übergeben Dateien
            LogMessage("Check existence of wsp files...", LogLevel.Normal);
            foreach (string solutionFileName in solutionFileNames)
            {
                LogMessage("Checking solution '" + solutionFileName + "'", LogLevel.Low);
                if (!File.Exists(solutionFileName))
                {
                    exceptions.Add("Solution '" + solutionFileName + "' not found.");
                }
            }

            //b. Existieren die Lösungen bereits auf dem Server
            LogMessage("Check for existing solutions in farm...", LogLevel.Normal);  
            foreach (string solutionName in solutionNames)
            {   LogMessage("Checking '" + solutionName + "'", LogLevel.Low);
                if (!SolutionExistsOnServer(solutionName))
                {
                    exceptions.Add("Solution '" + solutionName + "' does not exist on server");
                }
            }

            LogMessage(FormatHeader("Check preconditions finished successfully"), LogLevel.High);
            LogMessage("", LogLevel.High);
        }

        public override void PrepareSystem()
        {
            
        }

        public override void RunOperation()
        {
            LogMessage(FormatHeader("Upgrading solutions in farm"), LogLevel.High);

            //ok, parameters and preconditions are valid
            foreach (string solutionPath in solutionFileNames)
            {
                LogMessage("Upgrading " + Path.GetFileName(solutionPath) + "...", LogLevel.Normal);
                try
                {
                    SPSolution spSolution = GetSPSolutionFromPath(solutionPath);
                    spSolution.Upgrade(solutionPath);
                }
                catch (Exception ex)
                {
                    exceptions.Add("Solution '" + solutionPath + "' could not be updated (" + ex.Message + ")");
                }
            }

            HandleExceptions();

            LogMessage(FormatHeader("Upgrading solutions in farm successfully"), LogLevel.High);
            LogMessage("", LogLevel.High);
        }
                
        public override void ValidateSystem()
        {
            // TODO: Check that all solutions are deployed
        }
    }
}
